package com.banshuibao.bsb_android.http;

import android.text.TextUtils;

import com.banshuibao.bsb_android.MyApplication;

import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by zyh on 2017/2/13.
 */

public class OkHttpGetRequest implements ObservableOnSubscribe<String> {

    private String url;

    public OkHttpGetRequest(String url) {
        this.url = url;
    }

    private String getCookieID() {
        String cookieId = MyApplication.applicationInstance.getCookieId();
        return TextUtils.isEmpty(cookieId) ? "" : cookieId;
    }

    @Override
    public void subscribe(ObservableEmitter<String> e) throws Exception {
        Request request = new Request.Builder().
                url(url).
                header("cookieId", getCookieID()).
                get().
                build();
        Response response = OkHttpClientUtil.getInstance().getClient().newCall(request).execute();
        if (response.code() == 200)
            e.onNext(response.body().string());
        else
            e.onError(new Throwable("NET_ERROR"));
        e.onComplete();
    }
}
